<template>
	<view class="confirmOrder">
		<!-- 等待支付 -->
		<view class="status">
			等待支付
		</view>
		<!-- 地址 -->
		<view class="selectAddress" @tap="goAddress" v-if="this.address.id == undefined">
			去选择收货地址
		</view>
		<view class="address-block" @tap="goAddress" v-else>
			<view class="name-tel">
				姓名:{{ address.name }} 电话号码: {{ address.phoneNumber }}
			</view>
			<view class="address">
				收货地址: {{ address.province }}-{{ address.city }}-{{ address.region }} {{ address.detailAddress }}
			</view>
		</view>
		<!-- 订单 -->
		<view class="card">
			<view class="title">
				<view class="number">订单编号: {{ orderSn }}</view>
				<view class="text">等待支付</view>
			</view>
			<view class="block" v-for="items in orderList" :key="items.id">
				<image class="img" :src="items.productPic"></image>
				<view class="info">
					<view class="p p1">
						<view class="name">{{ items.productName }}</view>
						<view class="price">￥{{ items.productPrice }}元</view>
					</view>
					<view class="p p2">
						<view class="left">编号 : {{ items.productSn }}</view>
						<view class="right">x {{ items.productQuantity }}</view>
					</view>
					<view class="p p3">
						<view class="text">规格 : </view>
						<view class="productAttr"  v-for="item in JSON.parse(items.productAttr)" :key="item.key">
							<view class="text">{{ item.key }} : {{ item.value }}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="total">合计: {{ total }}元</view>
			<view class="btns">
				<view class="btn" @tap="goPay">确认付款</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getPreOrderById,addConfirmOrder } from "../../../api/order/index.js";
	import mix from "../../../mixins/index.js";
	export default {
		mixins: [mix],
		data() {
			return {
				orderList: [], // 订单列表
				orderSn: "", // 订单编号
				isShow: false, // 合计
				address: {}, // 地址
				addressId: "", // 订单 - 邮寄地址id
				orderId: "", // 订单 - 订单id
				payType: 0, // 订单 - 支付方式：0->未支付；1->支付宝；2->微信
				sourceType: 1, // 订单 - 订单来源：0->PC订单；1->app订单
			};
		},
		onShow() {
			// 判断 - 是否登陆
			if(this.checkLogin()){
				// 本地存储 - 获取地址
				this.address = uni.getStorageSync("selectAddress")
			}
		},
		onLoad(options) {
			this.orderId = options.id;
			// 判断 - 是否登陆
			if(this.checkLogin()){
				// 根据订单id获取订单明细
				getPreOrderById(options.id).then(res=>{
					this.orderSn = res.data.data.orderBase.orderSn;
					this.orderList = res.data.data.orderItems;
				})
			}
		},
		computed:{
			// 合计
			total(){
				return this.orderList.reduce((total,current)=>{
					return total + current.productPrice * current.productQuantity
				},0)
			}
		},
		methods:{
			// 跳转 - 支付页面
			goPay(){
				// 提交确认订单 - 参数
				var orderItemList = [];
				// 参数 - orderItemList - 可能为多个
				this.orderList.forEach(ele=>{
					var obj = {
						"productId": ele.productId,
						"productQuantity": ele.productQuantity,
						"productSkuId": ele.productSkuId
					}
					orderItemList.push(obj)
				});
				this.addressId = uni.getStorageSync("selectAddress").id;
				var obj = {
					"addressId": this.addressId,
					"orderId": this.orderId,
					"orderItemList": orderItemList,
					"payType": this.payType,
					"sourceType": this.sourceType
				};
				// 提交确认订单
				addConfirmOrder(obj).then(res=>{
					// 跳转 - 付款页面
					if(res.data.success){
						uni.navigateTo({
							url: `./payment/payment?orderId=${res.data.data.orderId}`
						})
					}
				})
			},
			// 跳转 - 地址页面
			goAddress(){
				uni.navigateTo({
					url: "../../user/address/address?type=select"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
@import "./index.scss"
</style>
